package com.yylending.las.lending.quota.initdata;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IEvent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.Global;
import com.yylending.las.YJKD;
import com.yylending.las.common.utils.preferenceutils.PreferenceConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by laixj on 2016/11/10.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class InitDataPresenter extends Presenter<InitDataModel, InitDataPresenter.View> {
	private final static String TAG = "InitDataPresenter";

	public interface View extends IView {
		/**
		 * 初始化选项卡
		 */
		void initOptionsSuccess(InitDataModel.Options options);

		void initOptionsFail(String msg);
	}

	//todo 考虑删掉
	public static class Event extends IEvent {
		final static int GET_SUPPORT_AREA_ERROR = 2;
		public final static int GET_SUPPORT_AREA_SUCCESS = 3;

		final static int GET_OPTIONS_ERROR = 4;
		public final static int GET_OPTIONS_SUCCESS = 5;

		public Event(int eventId) {
			id = eventId;
		}
	}

	public static class GetAreaEvent extends IEvent {
		public GetAreaEvent(int eventId) {
			id = eventId;
		}
	}

	public static class GetOptionsEvent extends IEvent {
		public GetOptionsEvent(int eventId) {
			id = eventId;
		}
	}

	private InitDataModel.Options mOptionsData = new InitDataModel.Options();

	public static class Generator implements PresenterFactory<InitDataPresenter> {
		@Override
		public InitDataPresenter create() {
			return new InitDataPresenter();
		}
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new InitDataModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	/**
	 * 获取选项内容
	 */
	public void getOptionList() {
		execute(null, InitDataModel.METHOD_GET_OPTION, false, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getOptionResult(InitDataModel.OptionsResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result) && null != result.getData()) {
				InitDataModel.Options rs = result.getData();
				CLog.i(TAG, "获取参数新rs=" + rs);
				mOptionsData.marriage.clear();
				mOptionsData.marriage.addAll(rs.marriage);
				mOptionsData.living.clear();
				mOptionsData.living.addAll(rs.living);
				mOptionsData.education.clear();
				mOptionsData.education.addAll(rs.education);
				mOptionsData.relation.clear();
				mOptionsData.relation.addAll(rs.relation);
				if(null != rs.loanLine){
					try{
						Global.mMinOnlineQuota = Integer.valueOf(rs.loanLine.loanLineAmount);
						Global.mMaxOnlineQuota = Integer.valueOf(rs.loanLine.highLoanLine);
						CLog.i(TAG, "获取参数Global.mMinOnlineQuota=" + Global.mMinOnlineQuota);
						CLog.i(TAG, "获取参数Global.mMaxOnlineQuota=" + Global.mMaxOnlineQuota);
					}catch (Exception e){
						CLog.i(TAG, "获取额度阈值失败");
					}
				}
				Gson gson = new Gson();
				String optionsStr = gson.toJson(mOptionsData, InitDataModel.Options.class);
				if (!TextUtils.isEmpty(optionsStr)) {
					EventAgent.postSticky(new GetOptionsEvent(Event.GET_OPTIONS_SUCCESS));
					CLog.i(TAG, "保存optionsStr=" + optionsStr);
					YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.KEY_OPTIONS, optionsStr);
					mView.initOptionsSuccess(mOptionsData);
				} else {
					YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.KEY_OPTIONS, "");
					mView.initOptionsFail(null);
				}
			} else {
				mView.initOptionsFail(null);
			}
		}
	}
}
